// fillings for Sport header
#include "Includes.hpp" // includes for project
#include "Sport.hpp" // class declarations

class Text_element; // there is a class 'Text_element'

////////////////////////////// CONSTRUCORS ///////////////////////////
Sport::Sport() { // default constructor should not be used
    cout << "\n\tyou are doing it wrong\n\tsee sport class\n"; // print error
}

Sport::Sport(char* name) : Text_element(name) {
    divisionLlist = new List(Sorted); // make a new division list for this sport
    pointmode = STANDARD;
}

Sport::Sport(ifstream* in, char* name) : Text_element(name) {
    int numdivisions; // number of divisions
    int pointmodenum; // number for setting pointmode correctly
    divisionLlist = new List(Sorted); // make a new division list for this sport

    in->ignore(2, '#'); // ignore delimiter
    (*in) >> pointmodenum; // read in pointmodenum
    pointmode = (PointMode)pointmodenum; // set pointmode
    in->ignore(2, '#'); // ignore delimiter
    (*in) >> numdivisions; // read in number of divisions
    in->ignore(2, '\n'); // ignore leftover crlf
    if (numdivisions > 0) {
	for (int dvar = 0; dvar <= numdivisions; dvar++) { // go through all divisions
	    divisionLlist->add(new Division(in, robio.readFileString(in))); // add a new division to list with
	}                                                                   // read name and file-stream
    } else {
	cout << "\n\tthere are no divisions in file (Sport::Sport(ifstream*,char*))"; // print error
    }
}

/////////////////////////// CLASS FUNCTIONS //////////////////////////
void Sport::display() {
	cout << "\n" << text << " with pointmode: " << pointmode << endl;
	divisionLlist->display_list();
}

char* Sport::getName() {
    return this->text;
}

void Sport::writeToFile(ofstream* out) {
    (*out) << text << " #"
	   << pointmode << " #"
	   << divisionLlist->no_of_elements() << endl;
    Division* tempdiv; // make temporary division to work on
    if (!(divisionLlist->is_empty())) { // check if list is empty
	for (int dvar = 0; dvar <= divisionLlist->no_of_elements(); dvar++) {
	    tempdiv = (Division*)divisionLlist->remove_no(dvar); // pull one out
	    tempdiv->writeToFile(out); // write to file
	    divisionLlist->add(tempdiv); // put it back in
	} // for dvar end
    } // if not empty
}

void Sport::printWriteTable(ostream* out, char* divisionname) {
    Division* tempdiv; // fer working
    if (strlen(divisionname) == 0) { // if no division name was entered
	for (int dvar = 0; dvar <= divisionLlist->no_of_elements(); dvar++) { // go through list
	    tempdiv = (Division*)divisionLlist->remove_no(dvar); // pull one out
		tempdiv->printWriteTable(out, pointmode); // print/write its table to screen/file
	    divisionLlist->add(tempdiv); // put it back into list
	} // for dvar end
    } else { // if division name was entered
	tempdiv = (Division*)divisionLlist->remove(divisionname); // remove division from list
	tempdiv->printWriteTable(out, pointmode); // print/write table
	divisionLlist->add(tempdiv); // add it back into list
    } // if strlen end
}
